package com.lemonsama.lesson2;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class ActionEventTest {
    public static void main(String[] args) {
        //开始按钮，实现同一个事件
        //开始  停止
        Frame frame = new Frame("开始-停止");
        Button button1 = new Button("start");
        Button button2 = new Button("stop");
        MyMonitor myMonitor = new MyMonitor();
        //可以显示定义触发返回的命令，不写则返回 label 的值
        //可以实现都个按钮只写一个监听类
        button2.setActionCommand("button2-stop");
        button1.addActionListener(myMonitor);
        button2.addActionListener(myMonitor);
        frame.add(button1, BorderLayout.NORTH);
        frame.add(button2, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
        closeWindow(frame);
    }

    public static void closeWindow(Frame frame) {
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
